// modules/goodsModule/pages/goodsdiscount/goodsdiscount.js
const util = require("../../../../utils/util.js")
const App = getApp();
const TYPE = {
  HOT: 'ishot',
  NEW : 'isnew',
  DISCOUNT: 'isdiscount',
  RECOMMAND: 'isrecommand'
}
Page({
  /**
   * 页面的初始数据
   */
  data: {
    navStyle: [],
    type: null,
    goodsList: [],
    currentPage: 1,
    pageSize: 10,
    scrollTop: 0,
    loadmore: 'end',
    loading: true,
    topImage: util.Background_base64('static/image/goods/discount.jpg'),
    empty_image: util.Background_base64('static/image/goods/noGoods.png'),
    position: App.globalData.position
  },

  //返回上一页
  navigateBack: function () {
    util.navigateBack();
  },

  //获取列表数据
  getDataList(isRefresh = false) {
    try {
      const params = {
        lat: this.data.position ? this.data.position.lat : 0,
        lng: this.data.position ? this.data.position.lng : 0,
        page: this.data.currentPage,
        limit: this.data.pageSize
      }
      const type = this.data.type;
      params[TYPE[type]] = 1;

      util
        .request("/api/goods/index", params, "GET")
        .then((res) => {
          this.setData({
            loadmore: 'end',
            loading: false
          });

          const result = res.data;
          if(result.code !== 0) return;

          const list = result.data;
          if (list.length > 0) {
            if(isRefresh){
              this.setData({
                goodsList: list /* 商品列表 */ ,
              });
            } else {
              this.setData({
                goodsList: this.data.goodsList.concat(list),
               });
            }

            wx.lin.renderWaterFlow(list, isRefresh ,()=>{
              this.setData({
                loadmore: 'end'
              });
            })
          }
        });
    } catch (err) {
      console.log(err);
      this.setData({
        loadmore: 'end'
      });
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    
    this.setData({
      type:options.type,
      navStyle: App.globalData,
    });
    console.log(this.data.navStyle);

    this.getDataList(true);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if(this.data.loadmore === 'loading') return;
    this.setData({
      loadmore: 'loading'
    });

    // 更新页码，准备获取下一页数据
    this.data.currentPage += 1;
    this.getDataList();
  },

  onPageScroll: function(e) {
    this.setData({
      scrollTop: e.scrollTop
    });
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})